package com.woniuxy.educationplatform.controller;

import com.woniuxy.educationplatform.bean.*;
import com.woniuxy.educationplatform.mapper.TestTypeMapper;
import com.woniuxy.educationplatform.service.IQuesService;
//import com.woniuxy.educationplatform.service.ITestService;
//import com.woniuxy.educationplatform.service.impl.TestServiceImpl;
import com.woniuxy.educationplatform.service.ITestService;
import com.woniuxy.educationplatform.util.ResultUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/test")
@RestController
public class TestController {
    @Resource
    private ITestService testServiceImpl;

    //悄悄塞进一个mapper
    @Resource
    private TestTypeMapper testTypeMapperImpl;


    /**
     * 查询所有试卷
     * @param
     * @return
     */
    @PostMapping(value="/all",produces = {"application/json;charset=utf-8"})
    public List<TestBean> getAll(String testName, String status, String testType){

        TestBean testBean = new TestBean();
        TestType testType1 = new TestType();
        testBean.setStatus(1);



        if((testName != null) && (!"".equals(testName) && (!"null".equals(testName)))){
            testBean.setTestName(testName);
        }

        if((status != null) && (!"".equals(status) && (!"null".equals(status)))){
            testBean.setStatus(Integer.valueOf(status));
        }

        if((testType != null) && (!"".equals(testType) && (!"null".equals(testType)))){
            testType1.setId(Integer.valueOf(testType));
        }

        testBean.setTestTypeBean(testType1);

        List<TestBean> testBeans = testServiceImpl.findAll(testBean);
        System.out.println(testBeans);
        return testBeans;
    }
   /* @RequestMapping(value="/all",produces = {"application/json;charset=utf-8"})
    public List<TestBean> findAll(){

        return testServiceImpl.findAll();
    }*/

    /**
     * 新增
     * @param testBean
     * @return
     */
    @PostMapping(value="/more",produces = {"application/json;charset=utf-8"})
    public ResultMsg addTest(TestBean testBean){
        ResultMsg resultMsg = new ResultMsg(200,"新增成功",null);
        //盐值 加密   (按照现实生活中：不同的人抄相同的菜，根据盐的分量来区别味道)
//        String pwd = new SimpleHash("MD5",adminBean.getPassword(),
//                adminBean.getLoginName(),1024).toString();
        testServiceImpl.addTestBean(testBean);
        return resultMsg;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping(value="/{ids}",produces = {"application/json;charset=utf-8"})
    public ResultMsg delete(@PathVariable("ids") Long[] ids){
        ResultMsg resultMsg = new ResultMsg(200,"删除成功",null);

        for (Long id:ids) {
            testServiceImpl.deleteTestBean(id);
        }
        return resultMsg;
    }

    /**
     * 修改题干
     * @param testBean
     * @param id
     * @return
     */
    @PutMapping(value="/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg updateTest(TestBean testBean, @PathVariable("id") Long id){
        System.out.println(testBean);
        ResultMsg resultMsg = new ResultMsg(200,"修改成功",null);
        testServiceImpl.updateTestBean(testBean);
        return resultMsg;
    }



    @RequestMapping(value="/selectbyid",produces = {"application/json;charset=utf-8"})
    public TestBean getOneById(Long id){
        return testServiceImpl.getOneById(id);
    }


    /**
     * 修改
     * @param testBean
     */
    @PostMapping(value="/update",produces = {"application/json;charset=utf-8"})
    public void updateTestBean(TestBean testBean){

        System.out.println(testBean);
        testServiceImpl.updateTestBean(testBean);

    }



    @GetMapping(value="/alltesttype",produces = {"application/json;charset=utf-8"})
    public List<TestType> selectAllTestType(){
        return testTypeMapperImpl.findAll();
    }

    /**
     * 删除试卷
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public ResultUtil delById(@PathVariable Integer id){
        ResultUtil resultUtil = new ResultUtil().setCode(ResultUtil.ERROR).setMessage("系统繁忙，请稍后再试");
        if((testServiceImpl.deleteTestBean(Long.valueOf(id)))>0){
            resultUtil.setCode(ResultUtil.SUCCESS).setMessage("删除成功");
        }
        return  resultUtil;
    }








}
